// 用户接口
const express = require("express");
const router = express.Router();
// MySql 链接池
const pool = require('../pool');



// 查询用户列表，接收数据 page 和 pagesize
router.get("/list", (req, res, next) => {
    if (!req.query.page) {
        req.query.page = 1;
    }
    if (!req.query.pagesize) {
        req.query.pagesize = 5;
    }

    let page = req.query.pagesize * (req.query.page - 1);
    let pagesize = Number(req.query.pagesize);

    let sql = 'select * from user limit ?, ?';
    pool.query(sql, [page, pagesize], (err, data) => {
        if (err) {
            return next(err);
        }
        pool.query("select count(*) count from user", (err, r) => {
            if (err) {
                return next(err);
            } else {
                res.send({ code: 200, data, msg: "已查询", page: Number(req.query.page), pagesize, total: r[0].count });
            }
        })
    });
});

// 新增用户，接收的参数为 name phone pwd address
router.post("/add", (req, res, next) => {
    let { name: uname, phone, pwd, address } = req.body;
    let d = new Date(); // 注册日期

    let sql = 'insert into user(uname, phone, pwd, register_date) values(?, ?, ?, ?)';
    pool.query(sql, [uname, phone, pwd, d], (err, data) => {
        if (err) {
            return next(err);
        }

        res.send({ code: 200, msg: "用户已添加" });
    })
});

// 根据查看用户详情，接收的值 uid
router.get("/details", (req, res) => {
    let { uid } = req.query;

    if (uid) {
        let sql = "select * from user where uid=?";
        pool.query(sql, [uid], (err, r) => {
            if (err) {
                return next(err);
            }
            res.send({ code: 200, data: r[0], msg: "已查询" });
        })
    } else {
        res.send({ code: 400, msg: "uid 的值是未知的" });
    }
});
// 根据查看用户详情，接收的值 phone
router.get("/phone", (req, res) => {
    let { phone } = req.query;

    if (phone) {
        let sql = "select * from user where phone=?";
        pool.query(sql, [phone], (err, r) => {
            if (err) {
                return next(err);
            }
            console.log(r, 'r')
            res.send({ code: 200, data: r[0], msg: "已查询" });
        })
    } else {
        res.send({ code: 400, msg: "phone 的值是未知的" });
    }
});
// 根据查看用户详情，接收的值 state
router.get("/state", (req, res) => {
    let { state } = req.query;

    if (state) {
        let sql = "select * from user where state=?";
        pool.query(sql, [state], (err, r) => {
            if (err) {
                return next(err);
            }
            // console.log(r,'111111111111')
            res.send({ code: 200, data: r[0], msg: "已查询" });
        })
    } else {
        res.send({ code: 400, msg: "state 的值是未知的" });
    }
});
// 删除用户，接收参数为 { uid }
router.post("/delete", (req, res) => {
    let { uid } = req.body;

    let sql = "DELETE FROM user WHERE uid = ?";
    pool.query(sql, [uid], (err, data) => {
        if (err) {
            return next(err);
        }

        if (data.affectedRows) {
            res.send({ code: 200, msg: "删除成功" });
        } else {
            res.send({ code: 400, msg: "删除失败" });
        }
    });
});

// 修改用户，接收参数 { uid, uname, phone, pwd, email, age, pic, address }
router.post("/modify", (req, res, next) => {
    console.log(1233)
    let { uid, uname, phone, pwd, email, age, pic, address, state } = req.body;
    let arr = [uname, phone, pwd, email, age, pic, address, state, uid];
    // console.log(req.body);
    let sql = `update user set
        uname = ?,
        phone = ?,
        pwd = ?,
        email = ?,
        age = ?,
        pic = ?,
        address = ?,
        state=?
        where uid = ?
    `;

    pool.query(sql, arr, (err, data) => {
        if (err) {
            return next(err);
        } else {
            // console.log(data);

            res.send({ code: 200, msg: "修改成功" });
        }
    });
});

// 通过搜索来查询用户，接收参数 { uname }
router.get("/search", (req, res, next) => {
    if (!req.query.page) {
        req.query.page = 1;
    }
    if (!req.query.pagesize) {
        req.query.pagesize = 5;
    }
    if (req.query.uname != "") {
        let uname = req.query.uname;
        let page = req.query.pagesize * (req.query.page - 1);
        let pagesize = Number(req.query.pagesize);
        // console.log(uname, page, pagesize, '@@')
        pool.query("select * from user where uname like ? limit ?, ?", [`%${uname}%`, page, pagesize], (err, data) => {
            if (err) {
                return next(err);
            } else {
                if (data.length == 0) {
                    res.send({ code: 400, msg: "未搜索到" });
                } else {
                    pool.query("select count(*) count from user where uname like ?", [`%${uname}%`], (err, r) => {
                        if (err) return next(err);
                        else {
                            res.send({ code: 200, data, msg: "已查询", page: Number(req.query.page), pagesize, total: r[0].count });
                        }
                    })
                }
            }
        });
    } else {
        res.send({ code: 400, msg: "user 的值是未知的" });
    }
});

// 用户登陆 { name, pwd }
router.post("/login", (req, res, next) => {
    let { name, pwd } = req.body;

    let sql = `select * from user where ${/^1[3-9]\d{9}$/.test(name) ? "phone" : "uname"} = ?`;
    pool.query(sql, [name], (err, r) => {
        if (err) return next(err)
        else {
            if (r.length == 0) {
                res.send({ code: 400, msg: "用户名错误" });
            } else {
                if (pwd == r[0].pwd) {
                    res.send({ code: 200, data: r[0], msg: "用户验证成功，可登陆" });
                } else {
                    res.send({ code: 400, msg: "密码错误" });
                }
            }
        }
    });
});

module.exports = router;